package org.file5.service.impl;

import org.apache.log4j.Logger;
import org.file5.dao.DaoFascade;
import org.file5.domain.BaseEntity;
import org.file5.domain.Entity2Entity;
import org.file5.domain.BaseEntity.STATUS;
import org.file5.domain.Entity2Entity.Relation;
import org.file5.service.BaseService;
import org.file5.util.DataBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.googlecode.objectify.Key;

@Service(value = "baseService")
public class BaseServiceImpl implements BaseService {
	private final Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	DaoFascade df;

	@Override
	public <E extends BaseEntity> E find(Class<E> entityClass, String uid) {
		return df.getBaseDao().find(entityClass, uid);
	}

//	@Override
//	public <E extends BaseEntity> E find(Class<E> entityClass, Key<E> key) {
//		return df.getBaseDao().find(entityClass, key);
//	}

	@Override
	public <E extends BaseEntity> E addUpdate(E entity) {
		return df.getBaseDao().addUpdate(entity);
	}

	@Override
	public <E extends BaseEntity> E find(Key<E> key) {
		return df.getBaseDao().find(key);
	}

	@Override
	public <E extends BaseEntity> DataBean<E> getListBy(Class<E> entityClass, int startPosition,
			int maxResult) {
		return df.getBaseDao().getListByProperties(entityClass, startPosition, maxResult);
	}



	@Override
	public <E extends BaseEntity> void remove(E entity) {
	df.getBaseDao().remove(entity);
	}

	@Override
	public <E extends BaseEntity> void remove(Class<E> entityClass, Long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public <E extends BaseEntity> E updateStatus(Class<E> entityClass, String uid,
			STATUS entityStatus) {
		E entity = df.getBaseDao().find(entityClass, uid);
		if(entity!=null){
			entity.setStatus(entityStatus);
			df.getBaseDao().addUpdate(entity);
		}
		return entity;
	}

	@Override
	public <E extends BaseEntity> void clearData(Class<E> entityClass) {
	df.getBaseDao().clearData(entityClass);
	}

	




	@Override
	public <E extends BaseEntity, E2 extends BaseEntity> Entity2Entity<E, E2> findEntity2Entity(
			E entity, Relation relation, E2 entity2) {
		return df.getBaseDao().findEntity2Entity(entity, relation, entity2);
	}

	@Override
	public <E extends BaseEntity, E2 extends BaseEntity> DataBean<Entity2Entity<E, E2>> getEntity2EntitiesBy(
			int startPosition, int maxResult, E entity, Relation relation,
			E2 entity2) {
		return df.getBaseDao().getEntity2EntitiesBy(startPosition, maxResult, entity, relation, entity2,null);
	}

	@Override
	public <E extends BaseEntity, E2 extends BaseEntity> DataBean<Entity2Entity<E, E2>> getEntity2EntitiesBy(
			int startPosition, int maxResult, Class<E> entityClass, String entityUid,
			Relation relation,Class<E2>entity2Class, String entity2Uid) {
		E entity = df.getBaseDao().find(entityClass, entityUid);
		E2 entity2 = df.getBaseDao().find(entity2Class, entity2Uid);
		return df.getBaseDao().getEntity2EntitiesBy(startPosition, maxResult, entity, relation, entity2,null);
	}

}
